package cn.elwy.rcp.ui.explorer.handler;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import com.yss.sofa.common.exception.RunException;
import com.yss.sofa.common.util.Assert;
import com.yss.sofa.ide.builder.dbscript.model.ScriptInfo;
import com.yss.sofa.ide.builder.dbscript.ui.internal.Config;
import com.yss.sofa.ide.builder.dbscript.ui.wizard.CreateDBManagerWizard;
import com.yss.sofa.ide.builder.packer.ui.project.ProjectUtil;
import com.yss.sofa.ide.model.ui.progress.RunnableAdapter;
import com.yss.sofa.ide.ui.dialog.MsgDialog;
import com.yss.sofa.ide.ui.dialog.ProgressDialog;

public class BuilderSQLHandler implements IHandler {

  private IResource resource;
  protected ScriptInfo scriptInfo;

  @Override
  public void addHandlerListener(IHandlerListener handlerListener) {
  }

  @Override
  public void dispose() {
  }

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell shell = HandlerUtil.getActiveShell(event);
    try {
      resource = (IResource) ProjectUtil.getSelection(event);
      Assert.notNull(resource, "请选择资源管理器向导的资源");
      boolean script = isScript(resource);
      if (script) {
        String taskName = "加载信息";
        Shell currentShell = Display.getCurrent().getActiveShell();
        int result = ProgressDialog.open(currentShell, getAdapterMethod(), taskName);
        if (result == 1) {
          WizardDialog dialog = new WizardDialog(shell, new CreateDBManagerWizard(scriptInfo));
          return dialog.open();
        }
      } else {
        WizardDialog dialog = new WizardDialog(shell, new CreateDBManagerWizard());
        return dialog.open();
      }
      return null;
    } catch (Exception e) {
      MsgDialog.openWarn(shell, e.getMessage());
      return null;
    }
  }

  private boolean isScript(IResource selection) {
    return selection.getFileExtension() != null && Config.SCRIPTE.endsWith(selection.getFileExtension());
  }

  public RunnableAdapter getAdapterMethod() {
    RunnableAdapter runnableAdapter = new RunnableAdapter() {

      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        try {
          scriptInfo = ScriptInfo.loadScriptInfo(resource.getLocation().toFile());
        } catch (RunException e) {
          throw e;
        } catch (Exception e) {
          throw new RunException("加载构建脚本配置失败！", e);
        }
      }
    };
    return runnableAdapter;
  }

  @Override
  public boolean isEnabled() {
    return true;// AssertUtil.isNotEmpty(ProjectUtil.getSelectionProject(null));
  }

  @Override
  public boolean isHandled() {
    return true;
  }

  @Override
  public void removeHandlerListener(IHandlerListener handlerListener) {
  }

}
